// pages/posts/post-detail.js
var postsData = require('../../../data/post-data.js');
var app = getApp();
Page({
    data: {
        isPlayingMusic: false,
    },
    onLoad: function (options) {
        var globalData = app.globalData;
        // 页面初始化 options为页面跳转所带来的参数
        var postId = options.id;
        console.log("postId=" + postId);
        this.data.currentPostId = postId;
        var postData = postsData.postList[postId];
        this.setData({
            postData: postData
        })

        var postsCollected = wx.getStorageSync('posts_collected')
        if (postsCollected) {
            var postCollected = postsCollected[postId];
            this.setData({
                collected: postCollected
            })
        }
        else {
            var postsCollected = {};
            postsCollected[postId] = false;
            wx.setStorageSync('posts_collected', postsCollected);
        }
        if (app.globalData.g_isPlayingMusic && (app.globalData.g_curentMusicPostId === postId)) {
            this.setData({ isPlayingMusic: true });
        }
        this.setMusicMonitor();
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    },
    setMusicMonitor: function () {
        var that = this;
        var currentPostId = this.data.currentPostId;

        wx.onBackgroundAudioPlay(function () {
            // callback          
            app.globalData.g_isPlayingMusic = true;
            if (app.globalData.g_curentMusicPostId === currentPostId) {
                // app.globalData.g_curentMusicPostId = currentPostId;
                that.setData({ isPlayingMusic: true });
            }
        });
        wx.onBackgroundAudioPause(function () {
            // callback 
            app.globalData.g_isPlayingMusic = false;
            if (app.globalData.g_curentMusicPostId === currentPostId) {
                // app.globalData.g_curentMusicPostId = null;
                that.setData({ isPlayingMusic: false });
            }
        });
        wx.onBackgroundAudioStop(function () {
            // callback 
            app.globalData.g_isPlayingMusic = false;
            if (app.globalData.g_curentMusicPostId === currentPostId) {
                // app.globalData.g_curentMusicPostId = null;
                that.setData({ isPlayingMusic: false });
            }
        });
    },
    // onCollectionTap: function (event) {
    //     var postsCollected = wx.getStorageSync('posts_collected');
    //     var postCollected = postsCollected[this.data.currentPostId];
    //     //收藏变成未收藏，未收藏变成收藏
    //     postCollected = !postCollected;
    //     postsCollected[this.data.currentPostId] = postCollected;
    //     this.showToast(postsCollected, postCollected);
    // },
    onCollectionTap: function (event) {
        this.getPostsCollectedSyc();
        //this.getPostsCollectedAsy();
    },
    getPostsCollectedSyc: function () {
        var postsCollected = wx.getStorageSync('posts_collected');
        var postCollected = postsCollected[this.data.currentPostId];
        // 收藏变成未收藏，未收藏变成收藏
        postCollected = !postCollected;
        postsCollected[this.data.currentPostId] = postCollected;
        this.showToast(postsCollected, postCollected);
    },
    getPostsCollectedAsy: function () {
        var that = this;
        wx.getStorage({
            key: "posts_collected",
            success: function (res) {
                var postsCollected = res.data;
                var postCollected = postsCollected[that.data.currentPostId];
                // 收藏变成未收藏，未收藏变成收藏
                postCollected = !postCollected;
                postsCollected[that.data.currentPostId] = postCollected;
                that.showToast(postsCollected, postCollected);
            }
        })
    },
    onShareTap: function (event) {
        console.log("onShareTap");
        var itemList = [
            "分享给微信好友",
            "分享到朋友圈",
            "分享到QQ",
            "分享到微博"
        ];
        wx.showActionSheet({
            itemList: itemList,
            itemColor: "#405f80",
            success: function (res) {
                // res.cancel 
                // res.tapIndex 数组元素序号 0开始
                wx.showModal({
                    title: "用户" + itemList[res.tapIndex],
                    content: "用户是否取消？" + res.cancel + "现在无法实现分享功能，什么时候能支持呢",
                })
            }
        })
    },
    showModal: function (postsCollected, postCollected) {
        var that = this;
        wx.showModal({
            title: "提示",
            content: postCollected ? "收藏该文章?" : "取消收藏该文章?",
            showCancel: true,
            cancelText: "取消",
            cancelColor: "#333",
            confirmText: "确认",
            confirmColor: "#405f80",
            success: function (res) {
                if (res.confirm) {
                    wx.setStorageSync('posts_collected', postsCollected);
                    //更新数据绑定
                    that.setData({
                        collected: postCollected
                    })
                }
            }
        })
    },
    showToast: function (postsCollected, postCollected) {
        wx.setStorageSync('posts_collected', postsCollected);
        //更新数据绑定
        this.setData({
            collected: postCollected
        })
        wx.showToast({
            title: postCollected ? "收藏成功" : "取消成功",
            duration: 1000,
            icon: "success"
        })
    },
    onMusicTap: function (event) {
        var isPlayingMusic = this.data.isPlayingMusic;
        var currentPostId = this.data.currentPostId;
        var postData = this.data.postData;
        if (isPlayingMusic) {
            wx.pauseBackgroundAudio({
                success: function (res) {
                    // success
                },
                fail: function () {
                    // fail
                },
                complete: function () {
                    // complete
                }
            })
            this.setData({ isPlayingMusic: false });
        } else {
            wx.playBackgroundAudio({
                dataUrl: postsData.postList[currentPostId].music.url,
                title: postData.music.title,
                coverImgUrl: postsData.postList[currentPostId].music.coverImg,
                success: function (res) {
                    // success
                },
                fail: function () {
                    // fail
                },
                complete: function () {
                    // complete
                }
            })
            this.setData({ isPlayingMusic: true });
            app.globalData.g_curentMusicPostId = currentPostId;
        }

    }
})